package com.shawn;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import redis.clients.jedis.Jedis;

public class RedisUtil {

	public static void main(String[] args) throws Exception {
		// host
		Jedis jedis = new Jedis("192.168.0.101");
		// password
		jedis.auth("Gepoint");
		System.out.println(jedis.ping());

		// string
		jedis.set("books", "java");
		System.out.println(jedis.get("books"));
		System.out.println(jedis.exists("books"));
		// expire time
		jedis.expire("books", 5);
		// java
		System.out.println(jedis.get("books"));
		// wait for 5s
		TimeUnit.SECONDS.sleep(5);
		// null
		System.out.println(jedis.get("books"));

		// list
		jedis.lpush("site-list", "Runoob");
		jedis.lpush("site-list", "Google");
		jedis.lpush("site-list", "Taobao");
		// 获取存储的数据并输出
		List<String> list = jedis.lrange("site-list", 0, 2);
		for (int i = 0; i < list.size(); i++) {
			System.out.println("列表项为: " + list.get(i));
		}

		// keys *
		Set<String> keys = jedis.keys("*");
		Iterator<String> it=keys.iterator() ;
		int num = 1;
		while(it.hasNext()){
			String key = it.next();
			System.out.println("第" + (num++) + "个key: " + key);
		}
	}
}
